package servletUtenteNonReg;

import dominio.Feedback;
import eccezioni.NessunFeedbackException;
import gestioneContesto.GestoreContesto;

import java.io.IOException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import logicaUtenteNonReg.GestoreInformazioniRemote;

/**
 * Servlet implementation class FeedbackNonRegServlet
 */
public class FeedbackNonRegServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private RequestDispatcher dispatcher;
	private GestoreContesto contesto;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FeedbackNonRegServlet() {
        
    	super();
        contesto = new GestoreContesto();
    
    }

    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			eseguiRichiesta(request, response);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			eseguiRichiesta(request, response);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@SuppressWarnings("deprecation")
	private void eseguiRichiesta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, NamingException {

		GestoreInformazioniRemote gestore = contesto.getGestore(GestoreInformazioniRemote.class, "GestoreInformazioniRemote");
		
		String username = request.getParameter("username");
		
		try {
			
			ArrayList<Feedback> listaFeedback = gestore.listaFeedback(username);
			
			for(int i = 0; i < listaFeedback.size(); i++) {
				
				request.setAttribute("mittente" + i, listaFeedback.get(i).getMittente());
				request.setAttribute("voto" + i, listaFeedback.get(i).getVoto());
				request.setAttribute("giorno" + i, listaFeedback.get(i).getData().getDate());
				request.setAttribute("mese" + i, listaFeedback.get(i).getData().getMonth());
				request.setAttribute("anno" + i, listaFeedback.get(i).getData().getYear());
				request.setAttribute("ore" + i,  listaFeedback.get(i).getData().getHours());
				request.setAttribute("minuti" + i, listaFeedback.get(i).getData().getMinutes());
				request.setAttribute("secondi" + i, listaFeedback.get(i).getData().getSeconds());
				request.setAttribute("commento" + i, listaFeedback.get(i).getCommento());
				
			}
			
			request.setAttribute("size", listaFeedback.size());
			request.setAttribute("username", username);
			dispatcher = request.getRequestDispatcher("ListaFeedbackNonReg.jsp");
			dispatcher.forward(request, response);
			
		}
		catch(NessunFeedbackException e) {
			
			request.setAttribute("size", 0);
			request.setAttribute("err", e.getMessaggio());
			dispatcher = request.getRequestDispatcher("ListaFeedbackNonReg.jsp");
			dispatcher.forward(request, response);
			
		}
		
	}

}
